﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tunnel.DevicesControls
{
    public partial class TS : DeviceBase
    {
        string deviceID;
        string shortName;
        string name;
        string typeID;
        string devicePos;
        int picX;
        int picY;
        string stationCode;
        string groupID;
        string commType;
        string protocol;
        string port;
        int factoryID;
        int controllable;
        DateTime updateTime;
        string updateBy;
        string standardID;
        string tunnel;


        string statusID;
        DateTime rsTime;
        string statusDescription;

        string paramAddr;
        string paramValue;
        DateTime cmdTime;
        string runned;

        //红灯变量ID
        string redid;
        //绿灯变量ID
        string greenid;
        //黄灯变量ID
        string yellowid;
        //转向灯变量ID
        string turnid;
        
        private void TS_DeviceIdChanged(object sender, EventArgs e)
        {
            string searchParamIDSQL = "select * from R_RunStatus where DeviceID='" + deviceID + "'";
            bwSearchParamID.RunWorkerAsync(searchParamIDSQL);
        }
        /// <summary>
        /// 定义DeviceID发生改变时发生的事件
        /// </summary>
        public event EventHandler DeviceIdChanged;

        public string DeviceID
        {
            get
            {
                return deviceID;
            }

            set
            {
                deviceID = value;
                DeviceIdChanged(this, new EventArgs());
                //string searchParamIDSQL = "select * from R_RunStatus where DeviceID='" + value + "'";
                //bwSearchParamID.RunWorkerAsync(searchParamIDSQL);
            }
        }

        

        public string ShortName
        {
            get
            {
                return shortName;
            }

            set
            {
                shortName = value;
            }
        }

        public string Name1
        {
            get
            {
                return name;
            }

            set
            {
                tsName.Text = value;
                name = value;
            }
        }

        public string TypeID
        {
            get
            {
                return typeID;
            }

            set
            {
                typeID = value;
            }
        }

        public string DevicePos
        {
            get
            {
                return devicePos;
            }

            set
            {
                devicePos = value;
            }
        }

        public int PicX
        {
            get
            {
                return picX;
            }

            set
            {
                picX = value;
            }
        }

        public int PicY
        {
            get
            {
                return picY;
            }

            set
            {
                picY = value;
            }
        }

        public string StationCode
        {
            get
            {
                return stationCode;
            }

            set
            {
                stationCode = value;
            }
        }

        public string GroupID
        {
            get
            {
                return groupID;
            }

            set
            {
                groupID = value;
            }
        }

        public string CommType
        {
            get
            {
                return commType;
            }

            set
            {
                commType = value;
            }
        }

        public string Protocol
        {
            get
            {
                return protocol;
            }

            set
            {
                protocol = value;
            }
        }

        public string Port
        {
            get
            {
                return port;
            }

            set
            {
                port = value;
            }
        }

        public int FactoryID
        {
            get
            {
                return factoryID;
            }

            set
            {
                factoryID = value;
            }
        }

        public int Controllable
        {
            get
            {
                return controllable;
            }

            set
            {
                controllable = value;
            }
        }

        public DateTime UpdateTime
        {
            get
            {
                return updateTime;
            }

            set
            {
                updateTime = value;
            }
        }

        public string UpdateBy
        {
            get
            {
                return updateBy;
            }

            set
            {
                updateBy = value;
            }
        }

        public string StandardID
        {
            get
            {
                return standardID;
            }

            set
            {
                standardID = value;
            }
        }

        public string Tunnel
        {
            get
            {
                return tunnel;
            }

            set
            {
                tunnel = value;
            }
        }

   

        public string StatusID
        {
            get
            {
                return statusID;
            }

            set
            {
                statusID = value;
            }
        }

        public DateTime RsTime
        {
            get
            {
                return rsTime;
            }

            set
            {
                rsTime = value;
            }
        }

        public string ParamAddr
        {
            get
            {
                return paramAddr;
            }

            set
            {
                paramAddr = value;
            }
        }

        public string ParamValue
        {
            get
            {
                return paramValue;
            }

            set
            {
                paramValue = value;
            }
        }

        public DateTime CmdTime
        {
            get
            {
                return cmdTime;
            }

            set
            {
                cmdTime = value;
            }
        }

        public string Runned
        {
            get
            {
                return runned;
            }

            set
            {
                runned = value;
            }
        }

        public string StatusDescription
        {
            get
            {
                return statusDescription;
            }

            set
            {
                if (value == "红灯")
                {
                    this.Image = Respic.红绿灯红灯;
                }
                if (value == "黄灯")
                {
                    this.Image = Respic.红绿灯黄灯;
                }
                if (value == "绿灯")
                {
                    this.Image = Respic.红绿灯绿灯;
                }
                if (value == "左转")
                {
                    this.Image = Respic.红绿灯左转;
                }
                if (value == "故障")
                {
                    this.Image = Respic.红绿灯故障;
                }
                statusDescription = value;
            }
        }

        public string RedID
        {
            get
            {
                return redid;
            }

            set
            {
                redid = value;
            }
        }

        public string GreenID
        {
            get
            {
                return greenid;
            }

            set
            {
                greenid = value;
            }
        }

        public string YellowID
        {
            get
            {
                return yellowid;
            }

            set
            {
                yellowid = value;
            }
        }

        public string TurnID
        {
            get
            {
                return turnid;
            }

            set
            {
                turnid = value;
            }
        }

        public TS()
        {
            InitializeComponent();
            this.DeviceIdChanged += TS_DeviceIdChanged;
            this.StatusDescription = "全灭";
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            tsName.Text = this.Name;
        }
        private void TS_DoubleClick(object sender, EventArgs e)

        {
            TSForm tf = new TSForm(this);
            tf.StartPosition = FormStartPosition.CenterScreen;
            tf.ShowDialog();
        }

        private void formOpen_Click(object sender, EventArgs e)
        {
            TSForm tf = new TSForm(this);
            tf.StartPosition = FormStartPosition.CenterScreen;
            tf.ShowDialog();
        }

        private void bwSearchParamID_DoWork(object sender, DoWorkEventArgs e)
        {
            SqlDBHelper SDH = new SqlDBHelper();
            DataTable dtParamIDTable = SDH.GetTable(e.Argument as string);
            e.Result = dtParamIDTable;   
        }

        private void bwSearchParamID_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            DataTable dtParamIDTable = e.Result as DataTable;
            foreach(DataRow dr in dtParamIDTable.Rows)
            {
                
                switch(dr["ParamStatus"].ToString())
                {
                    case "GREEN": GreenID = dr["ParamName"].ToString();break;
                    case "RED":  RedID = dr["ParamName"].ToString(); break;
                    case "YELLOW": YellowID = dr["ParamName"].ToString(); break;
                    case "TURN": TurnID = dr["ParamName"].ToString(); break;
                }
            }
        }
    }
}
